package com.example.coordinatorlayouttest;

import java.util.Stack;

/**
 * author : dut
 * e-mail : dt@xdja.com
 * date   : 2020/7/6
 * desc   :
 */
public class stackTest {

    public static void main(String[] args) {
        MyQueue queue = new MyQueue();

        queue.push(1);
        queue.push(2);
        queue.peek();  // 返回 1
        queue.pop();   // 返回 1
        queue.empty(); // 返回 false
        queue.push(3);
        queue.push(4);
        queue.peek();  // 返回 2
        queue.pop();   // 返回 2
    }

    static class MyQueue<T> {

        /**
         *  栈A 负责插入新元素
         */
        Stack<T> stackA;
        /**
         * 栈B 负责移除老元素
         */
        Stack<T> stackB;

        /** Initialize your data structure here. */
        public MyQueue() {
            stackA = new Stack<>();
            stackB = new Stack<>();
        }

        /** Push element x to the back of queue. */
        public void push(T t) {
            stackA.push(t);
        }

        /** Removes the element from in front of queue and returns that element. */
        public T pop() {
            if (stackB.isEmpty()) {
                if (stackA.isEmpty()) {
                    return null;
                }
                while (!stackA.isEmpty()) {
                    stackB.push(stackA.pop());
                }
            }
            return stackB.pop();
        }

        /** Get the front element. */
        public T peek() {
            if (stackB.isEmpty()) {
                if (stackA.isEmpty()) {
                    return null;
                }
                while (!stackA.isEmpty()) {
                    stackB.push(stackA.pop());
                }
            }
            return stackB.peek();
        }

        /** Returns whether the queue is empty. */
        public boolean empty() {
            return stackA.isEmpty() && stackB.isEmpty();
        }
    }
}
